<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--店员数据-->
      <el-col :span="24" :xs="24">
        <!-- 搜索条件 -->
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="店员名称" prop="userName">
            <el-input
              v-model="queryParams.userName"
              placeholder="请输入店员名称"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="关联单号" prop="orderNo">
            <el-input
              v-model="queryParams.orderNo"
              placeholder="请输入关联单号"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="店铺ID" prop="shopId" v-if="$store.getters && $store.getters.admin">
            <el-input
              v-model="queryParams.shopId"
              placeholder="请输入店铺ID"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker
              v-model="dateRange"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <!-- 操作按钮组 -->
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['employee:user:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['employee:user:export']"
            >导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <!-- 表格数据 -->
        <template>
          <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" @sort-change="handleSortChange">
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column type="index" label="序号" width="50" align="center" />
            <el-table-column label="店员" align="left" key="userId" prop="userId" :show-overflow-tooltip="true" min-width="180" >
              <template slot-scope="scope">
                <div class="flex-align-center" v-if="scope.row.employeeUser">
                  <el-avatar shape="circle" size="medium" :src="scope.row.employeeUser.avatar" alt="404"></el-avatar>
                  <div class="mgl5 font-14">{{scope.row.employeeUser.nickName}}</div>
                </div>
                <div v-else>-</div>
              </template>
            </el-table-column>
            <el-table-column label="金额" align="center" key="amount" prop="amount" sortable="custom" :sort-orders="['descending', 'ascending']" width="100"/>
            <el-table-column label="类型" align="center" key="financeType" prop="financeType" :show-overflow-tooltip="true">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.app_finance_type" :value="scope.row.financeType"/>
              </template>
            </el-table-column>
            <el-table-column label="关联订单" align="center" key="orderNo" prop="orderNo" width="225" />
            <el-table-column label="店铺名称" prop="shopInfo.shopName" v-if="$store.getters && $store.getters.admin" />
            <el-table-column label="创建时间" align="center" prop="createTime" width="160" sortable="custom" :sort-orders="['descending', 'ascending']">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.createTime) }}</span>
              </template>
            </el-table-column>
          </el-table>
          
          <!-- 分页 -->
          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </template>
      </el-col>
    </el-row>

  </div>
</template>

<script>
import { listFinance } from "@/api/employee/finance";

export default {
  name: "EmployeeFinance",
  dicts: ['app_finance_type'],
  components: { },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 店员表格数据
      dataList: null,
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        nickName: undefined,
        userName: undefined,
        orderNo: undefined,
        shopId: undefined,
        orderByColumn: 'createTime',
        isAsc: 'descending',
      },
      // 表单校验
      rules: {
      },
    };
  },
  watch: { 
    // 监听,当路由发生变化的时候执行
    '$route.query.userId'(newValue) {
      if(this.queryParams.userId != newValue){
        this.queryParams.userId = newValue
        this.getList()
      }
      // 执行其他操作或调用其他方法
    },
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询店员列表 */
    getList() {
      this.loading = true;
      listFinance(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.dataList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('employee/finance/export', {
        ...this.queryParams
      }, `店员财务_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>